/*
 * @Author: lihaogn
 * @Date: 2021-12-18 21:13:02
 * @LastEditTime: 2021-12-20 20:52:04
 * @LastEditor: lihaogn
 * @Description: 权限 controller
 * @FilePath: \Lix-admin-server\app\controller\permission.js
 */
const Controller = require("egg").Controller;
const { SuccessResponse, ErrorResponse } = require("../dto/responseDto");

class PermissionController extends Controller {
  /**
   * @author: lihaogn
   * @Date: 2021-12-20 00:57:59
   * @description: 查询权限
   * @param {*}
   * @return {*}
   */
  async index() {
    const { ctx } = this;
    try {
      const res = await ctx.service.permission.queryPermissions();
      if (res === false) {
        ctx.body = new ErrorResponse(null, "获取权限数据失败");
      } else {
        ctx.body = new SuccessResponse(res, "");
      }
    } catch (error) {
      console.log("获取权限信息err:", error);
    }
  }

  /**
   * @author: lihaogn
   * @Date: 2021-12-20 01:01:11
   * @description: 新增权限
   * @param {*}
   * @return {*}
   */
  async create() {
    const { ctx } = this;
    try {
      const res = await ctx.service.permission.createPermission(
        ctx.request.body
      );
      if (res === false) {
        ctx.body = new ErrorResponse(null, "新增失败");
      } else {
        ctx.body = new SuccessResponse(res, "新增成功");
      }
    } catch (error) {
      console.log("获取角色信息err:", error);
    }
  }

  /**
   * @author: lihaogn
   * @Date: 2021-12-20 01:01:30
   * @description: 修改权限
   * @param {*}
   * @return {*}
   */
  async update() {
    const { ctx } = this;

    const res = await ctx.service.permission.updatePermission(
      ctx.params.id,
      ctx.request.body
    );
    if (res === false) {
      ctx.body = new ErrorResponse(null, "修改失败");
    } else {
      ctx.body = new SuccessResponse(null, "修改成功");
    }
  }

  /**
   * @author: lihaogn
   * @Date: 2021-12-20 20:51:59
   * @description: 删除权限
   * @param {*}
   * @return {*}
   */
  async destroy() {
    const { ctx } = this;

    const res = ctx.service.permission.deletePermission(ctx.params.id);
    if (res === false) {
      ctx.body = new ErrorResponse(null, "删除失败");
    } else {
      ctx.body = new SuccessResponse(null, "删除成功");
    }
  }
}

module.exports = PermissionController;
